package mi.payment.utils.transaction;

import android.content.Context;
import android.util.Log;

import java.util.Map;
import java.util.Properties;

import mi.payment.utils.Config;
import mi.payment.utils.iso8583.MsgField;
import sunmi.sunmiui.utils.LogUtil;

/**
 * RID下载
 *
 * @author tomcat on 2017/2/17.
 */
public class CAPKDownloadTask extends TransactionTask<Void, Void, String> {

    private static final String TAG = "CAPKDownloadTask";

    protected CAPKDownloadTask(Context context) {
        super(context, false);
    }

    @Override
    protected void onBegin(Void[] params) {
    }

    @Override
    protected String appointResult() {
        return null;
    }

    @Override
    protected Map<String, MsgField> preAssembly() {
        return null;
    }

    @Override
    protected byte[] changeAndAssembly(Map<String, MsgField> fieldMap) {
        return null;
    }

    @Override
    protected String doTransactionSelf() {
        POSStatusUpload aidSeekTask = new POSStatusUpload(context);
        ParamsDownloadTask aidDownloadTask = new ParamsDownloadTask(context);

        resultStr = "FF";
        String rids = "";
        int ridSize = 0;
        Properties prop = new Properties();

        while (true) {
            String ridTags = aidSeekTask.syncExecute("372", String.valueOf(100 + ridSize));
            LogUtil.e(TAG, "rid_tag: " + ridTags);
            if (ridTags != null && ridTags.length() > 2) {
                rids += ridTags.substring(2);
                if (ridTags.substring(0, 2).equals("30") || ridTags.substring(0, 2).equals("31") || ridTags.substring(0, 2).equals("33")) {
                    break;
                }
                String[] RIDS = rids.split("9F06");
                ridSize = RIDS.length - 1;
            } else {
                return resultStr;
            }
        }
        LogUtil.e(TAG, "rid_tag_list: " + rids);

        // 下载RID并保存
        while (true) {

            if (rids.length() < 8) {
                break;
            }

            // RID[9F06]
            int idx9F06 = rids.indexOf("9F06", 4);
            int indexDF05 = rids.indexOf("DF05");

            String ret;
            String tRid = "";
            String[] params = new String[]{"370", tRid};

            if (idx9F06 > 0) {
                tRid = rids.substring(0, indexDF05);
                rids = rids.substring(idx9F06);
                params[1] = tRid;
                ret = aidDownloadTask.syncExecute(params);
            } else if (rids.length() > 0) {
                int indexOf = rids.indexOf("DF05");
                tRid = rids.substring(0, indexOf);
                rids = "";
                params[1] = tRid;
                ret = aidDownloadTask.syncExecute(params);
            } else {
                break;
            }

            LogUtil.e(TAG, "rid_response: " + ret);

            prop.setProperty(tRid, ret.substring(2).toUpperCase());

            resultStr = ret;
        }

        if (!prop.isEmpty()) {
            Config.saveRIDs(prop);
        }

        return resultStr;
    }


}
